package Alibaba;

/**
 * @author zhangmin
 * @create 2022-01-19 11:16
 * 5. 最长回文子串
 * 给你一个字符串 s，找到 s 中最长的回文子串。
 * 遍历每个元素为回文中心
 */
public class longestPalindrome5 {

    //获得已left和right为回文中心的最长回文字串
    String getLongestPalindrome(String s,int left,int right){
        int n=s.length();
        while (left>=0&&right<n&&s.charAt(left)==s.charAt(right)){
            left--;right++;
        }
        return s.substring(left+1,right);
    }

    public String longestPalindrome(String s) {
        String res="";
        for (int i = 0; i < s.length(); i++) {
            String s1 = getLongestPalindrome(s, i, i);
            String s2 = getLongestPalindrome(s, i, i + 1);
            res=s1.length()>res.length()?s1:res;
            res=s2.length()>res.length()?s2:res;
        }
        return res;
    }
}
